package com.wpp.controller;


//处理员工的增删改查的

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.regexp.internal.RE;
import com.wpp.pojo.Employee;
import com.wpp.pojo.Msg;
import com.wpp.service.EmployeeService;
import com.wpp.service.impl.EmployeeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Method;
import java.util.List;

@Controller
@RequestMapping("/emp")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeServiceImpl;
    //第一种方法查询数据
//    @RequestMapping("/emps")
//    public String getEmps(@RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model){
//
//        PageHelper.startPage(pn,5);
//        List<Employee> bean = employeeServiceImpl.GetFindAll();
//        PageInfo<Employee> pageInfo = new PageInfo<>(bean,5);
//
//        model.addAttribute("pageInfo",pageInfo);
//
//
//        System.out.println("当前页码" + pageInfo.getPageNum());
//        System.out.println("总页数" + pageInfo.getPages());
//        System.out.println("总记录数" + pageInfo.getTotal());
//        System.out.println("在页面需要连续显示的页码");
//        int[] navigatepageNums = pageInfo.getNavigatepageNums();
//        for (int i : navigatepageNums) {
//            System.out.println(" " + i);
//        }
//        List<Employee> list = pageInfo.getList();
//        for (Employee employee : list) {
//            System.out.println(employee);
//        }
//
//
//        return "list";
//    }

    @RequestMapping("/test1")
    public String test1() {
        return "emplist";
    }

    //第二种方法返回json格式的数据,查询全部员工
    @RequestMapping(value = "/emps", method = RequestMethod.GET)
    @ResponseBody
    public Msg getEmps(@RequestParam(value = "pn", defaultValue = "1") Integer pn) {
        PageHelper.startPage(pn, 5);
        List<Employee> bean = employeeServiceImpl.GetFindAll();
        PageInfo<Employee> pageInfo = new PageInfo<>(bean, 5);
        return Msg.success().add("pageinfo", pageInfo);
    }

    //新增员工
    @RequestMapping(value = "/emps", method = RequestMethod.POST)
    @ResponseBody
    public Msg insertData(Employee employee) {
        employeeServiceImpl.InsertData(employee);
        return Msg.success();
    }

    //根据id查询员工信息
    @RequestMapping(value = "/emps/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Msg GetData(@PathVariable("id") int id) {
        Employee employees = employeeServiceImpl.GetFindById(id);

        return Msg.success().add("emp", employees);
    }

    //根据id修改表数据
    @RequestMapping(value = "/emps/{empId}", method = RequestMethod.PUT)
    @ResponseBody
    public Msg updatedata(@PathVariable("empId") int id, Employee employee) {

        employeeServiceImpl.UpdateById(employee);
        return Msg.success();
    }


    //根据id删除表数据
    @RequestMapping(value = "/emps/{empId}", method = RequestMethod.DELETE)
    @ResponseBody
    public Msg Deletedata(@PathVariable("empId") int empId, Employee employee) {
        employeeServiceImpl.DeleteById(employee);
        return Msg.success();
    }


}
